home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / QuickDraw FX / events.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.9 KB  |  265 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        events.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Edgar Lee        
  7.  
  8.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/13/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include "FX.h"
  24.  
  25. void handleMouseDown();
  26.  
  27. void eventLoop()
  28. {
  29.     EventRecord     event;
  30.     WindowPtr        window;
  31.     short            clickArea;
  32.     Rect             screenRect;
  33.     long            sleep = 30;
  34.     Point            point;
  35.     
  36.     for (;;)
  37.     {        
  38.         if (WaitNextEvent( everyEvent, &event, sleep, (RgnHandle)nil ))
  39.         {
  40.             if (event.what == mouseDown)
  41.             {
  42.                 clickArea = FindWindow( event.where, &window );
  43.                 
  44.                 if (clickArea == inDrag)
  45.                 {
  46.                     //screenRect = (**GetGrayRgn()).rgnBBox;
  47.                     GetRegionBounds(GetGrayRgn(), &screenRect);
  48.                     DragWindow(window, event.where, &screenRect );
  49.                 }
  50.                 else if (clickArea == inGoAway)
  51.                 {
  52.                     if (TrackGoAway( window , event.where ))
  53.                         ExitToShell();
  54.                 }
  55.                 else if (clickArea == inMenuBar)
  56.                 {
  57.                     adjustMenus();
  58.                     handleMenu( MenuSelect( event.where ) );
  59.                 }
  60.                 else if (clickArea == inContent)
  61.                 {
  62.                     if (window != FrontWindow())
  63.                         SelectWindow( window );
  64.                     else
  65.                     {
  66.                         point = event.where;
  67.                         GlobalToLocal( &point );
  68.                         handleMouseDown( point );
  69.                     }
  70.                 }
  71.             }
  72.             else if (event.what == updateEvt)
  73.             {
  74.                 window = (WindowPtr)event.message;
  75.                 //SetPort( window );
  76.                 SetPortWindowPort( window );
  77.                 
  78.                 BeginUpdate( window );
  79.                 updateWindow();
  80.                 EndUpdate( window );
  81.             }
  82.             else if (event.what ==  keyDown || event.what == autoKey)
  83.             {
  84.                 if ((event.modifiers & cmdKey) != 0)
  85.                 {
  86.                     adjustMenus();
  87.                     handleMenu( MenuKey( (char)(event.message & charCodeMask) ) );
  88.                 }
  89.             }
  90.         }
  91.     }
  92. }
  93.  
  94. void handleMouseDown( Point point )
  95. {
  96.     int        i;
  97.     long    ticks;
  98.     int        itemNum = -1;
  99.     Rect    tempRect1, tempRect2;
  100.  
  101.     for (i = 0; i < numBItems; i++)
  102.         if (PtInRect( point, &bItem[i].rect ))
  103.         {
  104.             if (i == settings.bItem - 1)
  105.                 return;
  106.                 
  107.             drawItem( bItem[settings.bItem - 1].rect.left, bItem[settings.bItem - 1].rect.top,
  108.                                 &bItem[settings.bItem - 1].label, true, false );
  109.             drawItem( bItem[i].rect.left, bItem[i].rect.top,
  110.                                 &bItem[i].label, true, true );
  111.  
  112.             itemNum = 0;
  113.             settings.bItem = i + 1;
  114.             break;
  115.         }                
  116.  
  117.     if (gCurrentExample / 10 == transferID || gCurrentExample / 10 == customID)
  118.         for (i = 0; i < numTItems && itemNum == -1; i++)
  119.             if (PtInRect( point, &tItem[i].rect ))
  120.             {
  121.                 if (gCurrentExample % 10 != i + 1 || gCurrentExample / 10 == customID)
  122.                 {
  123.                     drawItem( tItem[settings.tItem - 1].rect.left, tItem[settings.tItem - 1].rect.top,
  124.                                 &tItem[settings.tItem - 1].label, true, false );
  125.                     drawItem( tItem[i].rect.left, tItem[i].rect.top,
  126.                                 &tItem[i].label, true, true );
  127.                     
  128.                     itemNum = i + 1;
  129.                     settings.tItem = itemNum;
  130.                 }                
  131.                 break;
  132.             }
  133.         
  134.     if (gCurrentExample / 10 == arithmeticID || gCurrentExample / 10 == customID)            
  135.         for (i = 0; i < numAItems && itemNum == -1; i++)
  136.             if (PtInRect( point, &aItem[i].rect ))
  137.             {
  138.                 if (gCurrentExample % 10 != i + 1 || gCurrentExample / 10 == customID)
  139.                 {
  140.                     drawItem( aItem[settings.aItem - 1].rect.left, aItem[settings.aItem - 1].rect.top,
  141.                                 &aItem[settings.aItem - 1].label, true, false );
  142.                     drawItem( aItem[i].rect.left, aItem[i].rect.top,
  143.                                 &aItem[i].label, true, true );
  144.                     
  145.                     itemNum = i + 1;
  146.                     settings.aItem = itemNum;
  147.                 }        
  148.                 break;
  149.             }
  150.     
  151.     if (gCurrentExample / 10 == colorizationID || gCurrentExample / 10 == customID)    
  152.         for (i = 0; i < numCItems && itemNum == -1; i++)
  153.             if (PtInRect( point, &cItem[i].rect ))
  154.             {
  155.                 if (gCurrentExample % 10 != i + 1 || gCurrentExample / 10 == customID)
  156.                 {
  157.                     drawItem( cItem[settings.cItem - 1].rect.left, cItem[settings.cItem - 1].rect.top,
  158.                                 &cItem[settings.cItem - 1].label, true, false );
  159.                     drawItem( cItem[i].rect.left, cItem[i].rect.top,
  160.                                 &cItem[i].label, true, true );
  161.                     
  162.                     itemNum = i + 1;
  163.                     settings.cItem = itemNum;
  164.                 }
  165.                 break;
  166.             }
  167.         
  168.     if (gCurrentExample / 10 == ditherID || gCurrentExample / 10 == customID)
  169.         for (i = 0; i < numDItems && itemNum == -1; i++)
  170.             if (PtInRect( point, &dItem[i].rect ))
  171.             {
  172.                 if (gCurrentExample % 10 != i + 1 || gCurrentExample / 10 == customID)
  173.                 {
  174.                     drawItem( dItem[settings.dItem - 1].rect.left, dItem[settings.dItem - 1].rect.top,
  175.                                 &dItem[settings.dItem - 1].label, true, false );
  176.                     drawItem( dItem[i].rect.left, dItem[i].rect.top,
  177.                                 &dItem[i].label, true, true );
  178.                     
  179.                     itemNum = i + 1;
  180.                     settings.dItem = itemNum;
  181.                 }
  182.                 break;
  183.             }
  184.     
  185.     if (gCurrentExample / 10 == searchProcID || gCurrentExample / 10 == customID)
  186.         for (i = 0; i < numMItems && itemNum == -1; i++)
  187.             if (PtInRect( point, &mItem[i].rect ))
  188.             {
  189.                 if (gCurrentExample % 10 != i + 1 || gCurrentExample / 10 == customID)
  190.                 {
  191.                     drawItem( mItem[settings.mItem - 1].rect.left, mItem[settings.mItem - 1].rect.top,
  192.                                 &mItem[settings.mItem - 1].label, true, false );
  193.                     drawItem( mItem[i].rect.left, mItem[i].rect.top,
  194.                                 &mItem[i].label, true, true );
  195.                     
  196.                     itemNum = i + 1;
  197.                     settings.mItem = itemNum;
  198.                 }    
  199.                 break;
  200.             }
  201.  
  202.     if (gCurrentExample / 10 == paintBucketID)
  203.     {
  204.         Rect    rect;
  205.         void    paintBucketExample();
  206.         
  207.         /*SetRect( &rect, (*gWindow).portRect.right - (*gGWorld).portRect.right - 20, 37,
  208.                 (*gWindow).portRect.right - 20, 37 + (*gGWorld).portRect.bottom );*/
  209.         
  210.         GetPortBounds(GetWindowPort(gWindow), &tempRect1);
  211.         GetPortBounds(gGWorld, &tempRect2);
  212.         SetRect( &rect, tempRect1.right - tempRect2.right - 20, 37,
  213.                 tempRect1.right - 20, 37 + tempRect2.bottom);
  214.  
  215.         paintBucketExample( &rect, 0, point );
  216.     }
  217.     /*
  218.     if (gCurrentExample / 10 == lassoID)
  219.     {
  220.         Rect    rect;
  221.         Rect    frame;
  222.         void    lassoToolExample();
  223.         
  224.         frame.left = point.h;
  225.         frame.top = point.v;
  226.         
  227.         while( StillDown() )
  228.         {
  229.             GetMouse( &point );
  230.             frame.right = point.h;
  231.             frame.bottom = point.v;
  232.         }
  233.         
  234.         SetRect( &rect, (*gWindow).portRect.right - (*gGWorld).portRect.right - 20, 37,
  235.                 (*gWindow).portRect.right - 20, 37 + (*gGWorld).portRect.bottom );
  236.  
  237.         lassoToolExample( &rect, 0, &frame );
  238.     }
  239.     */
  240.     if (itemNum != -1)
  241.     {
  242.         if (itemNum == 0)
  243.         {
  244.             drawSourceImage();
  245.             ticks = drawFXImage();
  246.             drawTime( ticks );
  247.         }
  248.         else
  249.         {
  250.             if (gCurrentExample / 10 == customID)
  251.                 itemNum = gCurrentExample;
  252.             else
  253.                 itemNum = ((gCurrentExample / 10) * 10) + itemNum;
  254.                 
  255.             if (itemNum != gCurrentExample || itemNum / 10 == customID)
  256.             {
  257.                 gCurrentExample = itemNum;
  258.                 
  259.                 drawSourceImage();
  260.                 ticks = drawFXImage();
  261.                 drawTime( ticks );
  262.             }
  263.         }
  264.     }
  265. }